#include "idcardread.h"
#include <QTextCodec>
#include "Syn_IDCardRead.h"
#include "Trf32.h"
#include "HDstdapi.h"

IDCardRead::IDCardRead(QObject *cr)
{
    port = -1;
    qRegisterMetaType<IdCData>("IdCData");
}

#if __T10DEV
bool IDCardRead::open()
{
    for(int j=1001; j<1017; j++)
    {
        if (Syn_OpenPort(j) ==0)	// 打开USB
        {
            if ( Syn_GetSAMStatus(j, 0) == 0)	// 检测SAM_V
            {
                port = j;
                Syn_ClosePort(j);
                return true;
            }
            Syn_ClosePort(j);
        }
    }

    for(int i=1; i<17; i++)
    {
        if (Syn_OpenPort(i) ==0 )	// 打开串口
        {
            if ( Syn_GetSAMStatus(i, 1) == 0)	// 检测SAM_V
            {
                port = i;
                Syn_ClosePort(i);
                return true;
            }
            Syn_ClosePort(i);
        }
    }

    return false;
}

bool IDCardRead::read(IdCData &data)
{
    int nRet;
    unsigned char pucIIN[4];
    unsigned char pucSN[8];
    IDCardData idd;

    memset(pucIIN,0,4);
    memset(pucSN,0,8);
    memset(&idd, 0, sizeof(idd));

    if (Syn_OpenPort(port) != 0)
        return false;

    if (Syn_StartFindIDCard(port, pucIIN, 0) != 0)
        return false;

    if (Syn_SelectIDCard(port, pucSN, 0) != 0)
        return false;

    nRet = Syn_ReadMsg(port,0,&idd );
    if (nRet == 0)
    {
        if (strlen(idd.Sex) == 0 ||
            strlen(idd.Name) == 0 ||
            strlen(idd.IDCardNo) == 0)
            return false;

        if (idd.Sex[0] == '1')
        {
            data["sex"] = "男";
        }
        else
        {
            data["sex"] = "女";
        }

        data["name"] = gb2u(idd.Name);
        data["id"] = idd.IDCardNo;
    }
    Syn_ClosePort(port);

    return nRet == 0;
}
#endif

#ifdef __CRTDEV
bool IDCardRead::open()
{
    int i;

    for (i = 1001; i <= 1006; i ++)
    {
        if (HD_InitComm(i) == 0)
        {
            port = i;
            return true;
        }
    }

    return false;
}

bool IDCardRead::read(IdCData &data)
{
    int ret;
    IDCardData idd;
    char tmp[100];

    if (HD_Authenticate() != 0)
        return false;

    memset(&idd, 0, sizeof(idd));
    ret = HD_Read_BaseMsg(NULL, idd.Name,
                           idd.Sex, idd.Nation,
                           idd.Born, tmp,
                           idd.IDCardNo,
                           tmp,tmp,tmp
                           );
    if (ret == 0)
    {
        if (strlen(idd.Sex) == 0 ||
            strlen(idd.Name) == 0 ||
            strlen(idd.IDCardNo) == 0)
            return false;

        data["sex"] = gb2u(idd.Sex);
        data["name"] = gb2u(idd.Name);
        data["id"] = idd.IDCardNo;
    }

    return (ret == 0);
}
#endif

QString IDCardRead::gb2u(char *gb)
{
    QTextCodec *codec = QTextCodec::codecForName("GB2312");
    QString u;

    u = codec->toUnicode(gb, strlen(gb));
    u.replace(" ", "\0");

    return u;
}

void IDCardRead::run()
{
    IdCData idd;

    isrun = true;
    msleep(2000);

    if (!open())
        return;

    while (isrun)
    {
        if (read(idd))
        {
            emit send(idd);
        }
        msleep(600);
    }
}
